package com.example.shopping.me;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.example.shopping.R;
import com.example.shopping.utils.EditUserDialog;
import com.example.shopping.utils.SaveUserUtils;

import de.hdodenhof.circleimageview.CircleImageView;

public class PersonCenterActivity extends AppCompatActivity {
    TextView telTv,nickTv,nameTv,addressTv;
    CircleImageView headIv;
    UserBean.DataBean bean;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_center);
        initview();
        bean=getUserInfo();
        setViewData(bean);
    }

    private void setViewData(UserBean.DataBean bean) {
        telTv.setText(bean.getPhone());
        nickTv.setText(bean.getNickname());
        nameTv.setText(bean.getName());
        addressTv.setText(bean.getAddress());
    }

    private UserBean.DataBean getUserInfo() {
        UserBean.DataBean bean= SaveUserUtils.getUserinfo();
        if (bean==null){
            bean=SaveUserUtils.getUserToObj(this);
        }
        return  bean;
    }

    private void initview() {
        telTv=findViewById(R.id.center_tv_zh);
        nickTv=findViewById(R.id.center_tv_nick);
        nameTv=findViewById(R.id.center_tv_name);
        addressTv=findViewById(R.id.center_tv_address);
        headIv=findViewById(R.id.center_iv_touxiang);

    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.info_iv_back:
                break;
            case R.id.info_iv_save:
                 String nickStr=nickTv.getText().toString();
                String nameStr=nameTv.getText().toString();
                String addressStr=addressTv.getText().toString();
                UserBean.DataBean ub=new UserBean.DataBean(bean.getId(),bean.getPhone(),bean.getPassword(),nameStr,addressStr,nickStr,bean.getQuestion(),bean.getAnswer());

                break;
            case R.id.center_iv_touxiang:
                break;
            case  R.id.center_iv_edit_nick:
                String hint="请修改贝壳昵称：";
                showEditDidlog(hint,nickTv);
                break;
            case  R.id.center_iv_edit_name:
                hint="请修改收件姓名：";
                showEditDidlog(hint,nameTv);
                break;
            case R.id.center_iv_edit_address:
                hint="请修改收件地址：";
                showEditDidlog(hint,addressTv);
                break;
        }
    }

    private void showEditDidlog(String hint, final TextView tv) {
        final EditUserDialog dialog =new EditUserDialog(this);
        dialog.show();
        dialog.setTipHint(hint);
        dialog.setDialogWidth();
        dialog.setOnEnsureListener(new EditUserDialog.OnEnsureListener() {
            @Override
            public void onEnsure(String msg) {
                if (!TextUtils.isEmpty(msg)) {
                    tv.setText(msg);
                }
                dialog.cancel();
            }
        });
    }
}
